﻿using System;

namespace Safebrowsing
{
    /// <summary>
    /// Service point for the safebrowsing API
    /// </summary>
    public class SafebrowsingApiServicePoint
    {
        public SafebrowsingApiServicePoint(Uri dataRequestUri, Uri hashRequestUri)
        {
            DataRequestUri = dataRequestUri;
            HashRequestUri = hashRequestUri;
        }

        /// <summary>
        /// Uri for the data request
        /// </summary>
        public Uri DataRequestUri { get; private set; }

        /// <summary>
        /// Uri for the full hash request
        /// </summary>
        public Uri HashRequestUri { get; private set; }

        /// <summary>
        /// Creates default service point for Google Safe Browsing api
        /// </summary>
        /// <param name="apiKey"></param>
        /// <param name="applicationVersion"></param>
        /// <returns></returns>
        public static SafebrowsingApiServicePoint CreateDefaultServicePoint(string apiKey, string applicationVersion)
        {
            return new SafebrowsingApiServicePoint(new Uri(string.Format(Constants.DATA_REQUEST_URL, apiKey, applicationVersion, Constants.API_VERSION)),
                new Uri(string.Format(Constants.GET_HASH_REQUEST_URL, apiKey, applicationVersion, Constants.API_VERSION)));
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (SafebrowsingApiServicePoint)) return false;
            return Equals((SafebrowsingApiServicePoint) obj);
        }

        public override string ToString()
        {
            return string.Format("Service point. Data request uri: {0}. Hash request uri: {1}", DataRequestUri,
                                 HashRequestUri);
        }

        public bool Equals(SafebrowsingApiServicePoint other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.DataRequestUri, DataRequestUri) && Equals(other.HashRequestUri, HashRequestUri);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((DataRequestUri != null ? DataRequestUri.GetHashCode() : 0)*397) ^ (HashRequestUri != null ? HashRequestUri.GetHashCode() : 0);
            }
        }
    }
}